using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;
using Garlic.CC.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Garlic.DM.DataModel;

namespace Garlic.DAL
{
	/// <summary>
	/// DataAccess
	/// </summary>
	public partial class OperationLogDAL
	{
		#region Sql const
		public  const string TS_BAS_OPERATIONLOG_SELECT_BY_ID=" SELECT [operation_log_sn],[operation_time],[action_id],[operator],[operation_dXYil],[operation_parameter1],[operation_parameter2],[operation_parameter3],[operation_parameter4],[operation_parameter5],[operation_parameter6],[operation_parameter7],[operation_parameter8],[operation_parameter9],[operation_parameter10]"+
											" FROM [TS_BAS_OperationLog] with(nolock) "+
											" WHERE 1=1 " +
											" and [operation_log_sn] = @operation_log_sn";

											
		public  const string TS_BAS_OPERATIONLOG_SELECT=" SELECT "+
											"[operation_log_sn],"+
											"[operation_time],"+
											"[action_id],"+
											"[operator],"+
											"[operation_dXYil],"+
											"[operation_parameter1],"+
											"[operation_parameter2],"+
											"[operation_parameter3],"+
											"[operation_parameter4],"+
											"[operation_parameter5],"+
											"[operation_parameter6],"+
											"[operation_parameter7],"+
											"[operation_parameter8],"+
											"[operation_parameter9],"+
											"[operation_parameter10]"+
											" FROM [TS_BAS_OperationLog] with(nolock) {0}";
														
								
	
		public  const string TS_BAS_OPERATIONLOG_INSERT=" INSERT INTO [TS_BAS_OperationLog] WITH(ROWLOCK) ("+
										//	"[operation_log_sn],"+
											"[operation_time],"+
											"[action_id],"+
											"[operator],"+
											"[operation_dXYil],"+
											"[operation_parameter1],"+
											"[operation_parameter2],"+
											"[operation_parameter3],"+
											"[operation_parameter4],"+
											"[operation_parameter5],"+
											"[operation_parameter6],"+
											"[operation_parameter7],"+
											"[operation_parameter8],"+
											"[operation_parameter9],"+
											"[operation_parameter10]"+
										") VALUES ("+
										//	"@operation_log_sn,"+
											"@operation_time,"+
											"@action_id,"+
											"@operator,"+
											"@operation_dXYil,"+
											"@operation_parameter1,"+
											"@operation_parameter2,"+
											"@operation_parameter3,"+
											"@operation_parameter4,"+
											"@operation_parameter5,"+
											"@operation_parameter6,"+
											"@operation_parameter7,"+
											"@operation_parameter8,"+
											"@operation_parameter9,"+
											"@operation_parameter10"+
										");select @@Identity;";
									
									
		public  const string TS_BAS_OPERATIONLOG_UPDATE=" UPDATE [TS_BAS_OperationLog] with(rowlock) SET "+
											" [operation_time] = @operation_time, "+
											" [action_id] = @action_id, "+
											" [operator] = @operator, "+
											" [operation_dXYil] = @operation_dXYil, "+
											" [operation_parameter1] = @operation_parameter1, "+
											" [operation_parameter2] = @operation_parameter2, "+
											" [operation_parameter3] = @operation_parameter3, "+
											" [operation_parameter4] = @operation_parameter4, "+
											" [operation_parameter5] = @operation_parameter5, "+
											" [operation_parameter6] = @operation_parameter6, "+
											" [operation_parameter7] = @operation_parameter7, "+
											" [operation_parameter8] = @operation_parameter8, "+
											" [operation_parameter9] = @operation_parameter9, "+
											" [operation_parameter10] = @operation_parameter10 "+
										" WHERE 1=1 " +
											" and [operation_log_sn] = @operation_log_sn";
										
	
	
		public  const string TS_BAS_OPERATIONLOG_DELETE=" DELETE FROM [TS_BAS_OperationLog]" +
										" WHERE 1=1"+
											" and [operation_log_sn] = @operation_log_sn";
										
										
		
		#endregion
		
		/// <summary>
		/// Get OperationLogInfo by operation_log_sn
		/// </summary>
		/// <param name="operation_log_sn">OperationLogInfo </param>
		/// <returns></returns>
		public static OperationLogInfo GetInfo(int operation_log_sn)
		{
	
				Database db = DatabaseFactory.CreateDatabase();
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_OPERATIONLOG_SELECT_BY_ID);
			db.AddInParameter(dbCommand, "@operation_log_sn", DbType.Int32,operation_log_sn);
				

				using (IDataReader dr = db.ExecuteReader(dbCommand))
				{
					if (dr.Read())
					{
						return CreateOperationLogInfo(dr);
					}
					else
						return null;
				}
		}
	
	
		/// <summary>
		/// Get data list
		/// </summary>
		/// <param name="textWhere">conditon</param>
		/// <param name="orderText">sort</param>
		/// <returns>OperationLogInfo list </returns>
		public static List<OperationLogInfo> GetList(string textWhere,string orderText)
		{
			string query=string.Empty;
			if(!string.IsNullOrEmpty(textWhere))
			{
			query+=" where "+textWhere; 
			}

            if (!string.IsNullOrEmpty(orderText))
            {
                query += " order by " + orderText;
            }
            else
            {
                query += " order by Operation_log_sn desc";
            }
			
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(string.Format(TS_BAS_OPERATIONLOG_SELECT,query));
			
			return GetList(string.Format(TS_BAS_OPERATIONLOG_SELECT,query));
			
		}
		
		/// <summary>
		/// Get data list
		/// </summary>
		/// <param name="sql">SQL statement</param>
		/// <returns>OperationLogInfo list </returns>
		public static List<OperationLogInfo> GetList(string sql)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(sql);
			List<OperationLogInfo> list=new List<OperationLogInfo>();
			using (IDataReader dr = db.ExecuteReader(dbCommand))
			{
				while(dr.Read())
				{
					list.Add(CreateOperationLogInfo(dr));
				}
			}
			
			return list;
		}

	
		/// <summary>
		/// Add
		/// </summary>
		/// <param name="operationLogInfo"> entity</param>
		public static int Add(OperationLogInfo operationLogInfo)
		{
				Database db = DatabaseFactory.CreateDatabase();
	
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_OPERATIONLOG_INSERT);
				
				//db.AddInParameter(dbCommand, "@operation_log_sn",DbType.Int32,operationLogInfo.Operation_log_sn); 
				db.AddInParameter(dbCommand, "@operation_time",DbType.DateTime,operationLogInfo.Operation_time); 
				db.AddInParameter(dbCommand, "@action_id",DbType.Int32,operationLogInfo.Action_id); 
				db.AddInParameter(dbCommand, "@operator",DbType.AnsiString,operationLogInfo.Operator); 
				db.AddInParameter(dbCommand, "@operation_dXYil",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_dXYil)); 
				db.AddInParameter(dbCommand, "@operation_parameter1",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter1)); 
				db.AddInParameter(dbCommand, "@operation_parameter2",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter2)); 
				db.AddInParameter(dbCommand, "@operation_parameter3",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter3)); 
				db.AddInParameter(dbCommand, "@operation_parameter4",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter4)); 
				db.AddInParameter(dbCommand, "@operation_parameter5",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter5)); 
				db.AddInParameter(dbCommand, "@operation_parameter6",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter6)); 
				db.AddInParameter(dbCommand, "@operation_parameter7",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter7)); 
				db.AddInParameter(dbCommand, "@operation_parameter8",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter8)); 
				db.AddInParameter(dbCommand, "@operation_parameter9",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter9)); 
				db.AddInParameter(dbCommand, "@operation_parameter10",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter10)); 
				
				return int.Parse("0"+db.ExecuteScalar(dbCommand));
		}
		
		/// <summary>
		/// Update
		/// </summary>
		/// <param name="operationLogInfo"> utility</param>
		public static void Update(OperationLogInfo operationLogInfo)
		{
			Database db = DatabaseFactory.CreateDatabase();
	
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_OPERATIONLOG_UPDATE);
				
				db.AddInParameter(dbCommand, "@operation_log_sn",DbType.Int32,operationLogInfo.Operation_log_sn); 
				db.AddInParameter(dbCommand, "@operation_time",DbType.DateTime,operationLogInfo.Operation_time); 
				db.AddInParameter(dbCommand, "@action_id",DbType.Int32,operationLogInfo.Action_id); 
				db.AddInParameter(dbCommand, "@operator",DbType.AnsiString,operationLogInfo.Operator); 
				db.AddInParameter(dbCommand, "@operation_dXYil",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_dXYil)); 
				db.AddInParameter(dbCommand, "@operation_parameter1",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter1)); 
				db.AddInParameter(dbCommand, "@operation_parameter2",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter2)); 
				db.AddInParameter(dbCommand, "@operation_parameter3",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter3)); 
				db.AddInParameter(dbCommand, "@operation_parameter4",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter4)); 
				db.AddInParameter(dbCommand, "@operation_parameter5",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter5)); 
				db.AddInParameter(dbCommand, "@operation_parameter6",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter6)); 
				db.AddInParameter(dbCommand, "@operation_parameter7",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter7)); 
				db.AddInParameter(dbCommand, "@operation_parameter8",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter8)); 
				db.AddInParameter(dbCommand, "@operation_parameter9",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter9)); 
				db.AddInParameter(dbCommand, "@operation_parameter10",DbType.AnsiString,DBConvert.ToDBValue(operationLogInfo.Operation_parameter10)); 
				
				db.ExecuteNonQuery(dbCommand);
		}
		
		/// <summary>
		/// Delete
		/// </summary>
		/// <param name="operation_log_sn">TS_BAS_OperationLog Number</param>
		/// <returns></returns>
		public static void Delete(int operation_log_sn)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_OPERATIONLOG_DELETE);
			db.AddInParameter(dbCommand, "@operation_log_sn", DbType.Int32,operation_log_sn);
			db.ExecuteNonQuery(dbCommand);
		}
		
	

	
		private static OperationLogInfo CreateOperationLogInfo(IDataReader rdr)
		{
			OperationLogInfo myoperationLogInfo = new OperationLogInfo();
			myoperationLogInfo.Operation_log_sn=rdr.GetInt32(0);
			myoperationLogInfo.Operation_time=rdr.GetDateTime(1);
			myoperationLogInfo.Action_id=rdr.GetInt32(2);
			myoperationLogInfo.Operator=rdr.GetString(3);
			myoperationLogInfo.Operation_dXYil=DBConvert.GetString(rdr,4);
			myoperationLogInfo.Operation_parameter1=DBConvert.GetString(rdr,5);
			myoperationLogInfo.Operation_parameter2=DBConvert.GetString(rdr,6);
			myoperationLogInfo.Operation_parameter3=DBConvert.GetString(rdr,7);
			myoperationLogInfo.Operation_parameter4=DBConvert.GetString(rdr,8);
			myoperationLogInfo.Operation_parameter5=DBConvert.GetString(rdr,9);
			myoperationLogInfo.Operation_parameter6=DBConvert.GetString(rdr,10);
			myoperationLogInfo.Operation_parameter7=DBConvert.GetString(rdr,11);
			myoperationLogInfo.Operation_parameter8=DBConvert.GetString(rdr,12);
			myoperationLogInfo.Operation_parameter9=DBConvert.GetString(rdr,13);
			myoperationLogInfo.Operation_parameter10=DBConvert.GetString(rdr,14);

			return myoperationLogInfo;
		}
	}
}
